
gl res "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Results"

cap log close
log using "$res\log\setup_equipop.log", replace

* Läser in EquiPop-data för ethnic majority 
foreach year in 2002 2006 2010 {
clear
import delimited using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\From_EquiPop\Above17\ethnic_maj_`year'.dat",  varnames(1)
ren eastwest XKOORD_`year'
ren northsouth YKOORD_`year'
foreach k in 50 100 500 1000 2000{
destring intervalratio_`k', replace dpcomma
ren intervalratio_`k' Ratioethnic_maj_`year'_`k'
destring intervaldistance_`k', replace dpcomma
ren intervaldistance_`k' Distanceethnic_maj_`year'_`k'
}

sort XKOORD_`year' YKOORD_`year'
save "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\ethnic_maj_`year'_temp.dta", replace
}


* Analys ethnic majority
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\ethnic_maj_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

foreach k in 50 100 500 1000 2000{
bysort kommunkod: egen ethnic_maj_`k' = mean(Ratioethnic_maj_`year'_`k') 							/*Municipality-mean for non-politicians*/
bysort kommunkod: egen ethnic_maj_`k'_pol = mean(Ratioethnic_maj_`year'_`k') if politician_`year'==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_ethnic_maj_`k'= ethnic_maj_`k'_pol/ethnic_maj_`k'
}
collapse Ratio_ethnic_maj_50 Ratio_ethnic_maj_100 Ratio_ethnic_maj_500 Ratio_ethnic_maj_1000 Ratio_ethnic_maj_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_maj_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_maj_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_maj_`year'.dta"
}

collapse Ratio_ethnic_maj_50 Ratio_ethnic_maj_100 Ratio_ethnic_maj_500 Ratio_ethnic_maj_1000 Ratio_ethnic_maj_2000 
ren Ratio_ethnic_maj_50 Ethnic_majority50
ren Ratio_ethnic_maj_100 Ethnic_majority100
ren Ratio_ethnic_maj_500 Ethnic_majority500
ren Ratio_ethnic_maj_1000 Ethnic_majority1000
ren Ratio_ethnic_maj_2000 Ethnic_majority2000
gen id=1
reshape long Ethnic_majority, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Ethnic_majority.dta", replace


* Läser in EquiPop-data för high-educated 
foreach year in 2002 2006 2010 {
clear
import delimited using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\From_EquiPop\Above17\highed_`year'.dat",  varnames(1)
ren eastwest XKOORD_`year'
ren northsouth YKOORD_`year'
foreach k in 50 100 500 1000 2000{
destring intervalratio_`k', replace dpcomma
ren intervalratio_`k' RatioHighed_`year'_`k'
destring intervaldistance_`k', replace dpcomma
ren intervaldistance_`k' Distancehighed_`year'_`k'
}

sort XKOORD_`year' YKOORD_`year'
save "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highed_`year'_temp.dta", replace
}



* Analys highed
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highed_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

foreach k in 50 100 500 1000 2000{
bysort kommunkod: egen Highed_`k' = mean(RatioHighed_`year'_`k') 							/*Municipality-mean for non-politicians*/
bysort kommunkod: egen Highed_`k'_pol = mean(RatioHighed_`year'_`k') if politician_`year'==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_Highed_`k'= Highed_`k'_pol/Highed_`k'
}
collapse Ratio_Highed_50 Ratio_Highed_100 Ratio_Highed_500 Ratio_Highed_1000 Ratio_Highed_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_education_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_education_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_education_`year'.dta"
}

collapse Ratio_Highed_50 Ratio_Highed_100 Ratio_Highed_500 Ratio_Highed_1000 Ratio_Highed_2000 
ren Ratio_Highed_50 Highed50
ren Ratio_Highed_100 Highed100
ren Ratio_Highed_500 Highed500
ren Ratio_Highed_1000 Highed1000
ren Ratio_Highed_2000 Highed2000
gen id=1
reshape long Highed, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Highed.dta", replace


* Läser in EquiPop-data för high-earners (highest quartile)
foreach year in 2002 2006 2010 {
clear
import delimited using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\From_EquiPop\Above17\highq_`year'.dat",  varnames(1)
ren eastwest XKOORD_`year'
ren northsouth YKOORD_`year'
foreach k in 50 100 500 1000 2000{
destring intervalratio_`k', replace dpcomma
ren intervalratio_`k' RatioHighq_`year'_`k'
destring intervaldistance_`k', replace dpcomma
ren intervaldistance_`k' Distancehighq_`year'_`k'
}

sort XKOORD_`year' YKOORD_`year'
save "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highq_`year'_temp.dta", replace
}


* Analys highq
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highq_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

foreach k in 50 100 500 1000 2000{
bysort kommunkod: egen Highq_`k' = mean(RatioHighq_`year'_`k') 							/*Municipality-mean for non-politicians*/
bysort kommunkod: egen Highq_`k'_pol = mean(RatioHighq_`year'_`k') if politician_`year'==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_Highq_`k'= Highq_`k'_pol/Highq_`k'
}
collapse Ratio_Highq_50 Ratio_Highq_100 Ratio_Highq_500 Ratio_Highq_1000 Ratio_Highq_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_dispinc_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_dispinc_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_dispinc_`year'.dta"
}

collapse Ratio_Highq_50 Ratio_Highq_100 Ratio_Highq_500 Ratio_Highq_1000 Ratio_Highq_2000  
ren Ratio_Highq_50 Highq50
ren Ratio_Highq_100 Highq100
ren Ratio_Highq_500 Highq500
ren Ratio_Highq_1000 Highq1000
ren Ratio_Highq_2000 Highq2000
gen id=1
reshape long Highq, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Highq.dta", replace

******************************
*All politicians: ADVANTAGE **
******************************

* Analys ethnic majority
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\ethnic_maj_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

foreach k in 50 100 500 1000 2000{
bysort kommunkod: egen ethnic_maj_`k' = mean(Ratioethnic_maj_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen ethnic_maj_`k'_pol = mean(Ratioethnic_maj_`year'_`k') if politician_`year'==1 						/*Ditto for politicians*/
bysort kommunkod: gen Ratio_ethnic_maj_`k'= ethnic_maj_`k'_pol/ethnic_maj_`k'
}
collapse Ratio_ethnic_maj_50 Ratio_ethnic_maj_100 Ratio_ethnic_maj_500 Ratio_ethnic_maj_1000 Ratio_ethnic_maj_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_maj_all_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_maj_all_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_maj_all_`year'.dta"
}
*sum
*scatter Ratio_ethnic_maj_50 Ratio_ethnic_maj_500 Ratio_ethnic_maj_1000 Ratio_ethnic_maj_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_ethnic_maj.pdf", as(pdf) 

collapse Ratio_ethnic_maj_50 Ratio_ethnic_maj_100 Ratio_ethnic_maj_500 Ratio_ethnic_maj_1000 Ratio_ethnic_maj_2000 
ren Ratio_ethnic_maj_50 Ethnic_majority50
ren Ratio_ethnic_maj_100 Ethnic_majority100
ren Ratio_ethnic_maj_500 Ethnic_majority500
ren Ratio_ethnic_maj_1000 Ethnic_majority1000
ren Ratio_ethnic_maj_2000 Ethnic_majority2000
gen id=1
reshape long Ethnic_majority, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Ethnic_majority_all.dta", replace



* Analys highed
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highed_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

foreach k in 50 100 500 1000 2000{
bysort kommunkod: egen Highed_`k' = mean(RatioHighed_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen Highed_`k'_pol = mean(RatioHighed_`year'_`k') if politician_`year'==1						/*Ditto for politicians*/
bysort kommunkod: gen Ratio_Highed_`k'= Highed_`k'_pol/Highed_`k'
}
collapse Ratio_Highed_50 Ratio_Highed_100 Ratio_Highed_500 Ratio_Highed_1000 Ratio_Highed_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_education_all_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_education_all_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_education_all_`year'.dta"
}

*sum
*scatter Ratio_Highed_50 Ratio_Highed_500 Ratio_Highed_1000 Ratio_Highed_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_highed.pdf", as(pdf) 
collapse Ratio_Highed_50 Ratio_Highed_100 Ratio_Highed_500 Ratio_Highed_1000 Ratio_Highed_2000 
ren Ratio_Highed_50 Highed50
ren Ratio_Highed_100 Highed100
ren Ratio_Highed_500 Highed500
ren Ratio_Highed_1000 Highed1000
ren Ratio_Highed_2000 Highed2000
gen id=1
reshape long Highed, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Highed_all.dta", replace




* Analys highq
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highq_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

foreach k in 50 100 500 1000 2000{
bysort kommunkod: egen Highq_`k' = mean(RatioHighq_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen Highq_`k'_pol = mean(RatioHighq_`year'_`k') if politician_`year'==1						/*Ditto for politicians*/
bysort kommunkod: gen Ratio_Highq_`k'= Highq_`k'_pol/Highq_`k'
}
collapse Ratio_Highq_50 Ratio_Highq_100 Ratio_Highq_500 Ratio_Highq_1000 Ratio_Highq_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_dispinc_all_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_dispinc_all_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_dispinc_all_`year'.dta"
}
*sum
*scatter Ratio_Highq_50 Ratio_Highq_100 Ratio_Highq_500 Ratio_Highq_1000 Ratio_Highq_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_highq.pdf", as(pdf) 

collapse Ratio_Highq_50 Ratio_Highq_100 Ratio_Highq_500 Ratio_Highq_1000 Ratio_Highq_2000  
ren Ratio_Highq_50 Highq50
ren Ratio_Highq_100 Highq100
ren Ratio_Highq_500 Highq500
ren Ratio_Highq_1000 Highq1000
ren Ratio_Highq_2000 Highq2000
gen id=1
reshape long Highq, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Highq_all.dta", replace






***************************
*Left politicians
***************************
clear all

* Analys ethnic majority
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\ethnic_maj_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

gen parti_low = lower(parti_initial)
gen vald_left = 0
replace vald_left = vald==1 & (parti_low =="s" | parti_low =="g" | parti_low =="v")

foreach k in 50 100 500 1000 2000{
bysort kommunkod: egen ethnic_maj_`k' = mean(Ratioethnic_maj_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen ethnic_maj_`k'_pol = mean(Ratioethnic_maj_`year'_`k') if vald_left==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_ethnic_maj_`k'= ethnic_maj_`k'_pol/ethnic_maj_`k'
}
collapse Ratio_ethnic_maj_50 Ratio_ethnic_maj_100 Ratio_ethnic_maj_500 Ratio_ethnic_maj_1000 Ratio_ethnic_maj_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_maj_left_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_maj_left_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_maj_left_`year'.dta"
}
*sum
*scatter Ratio_ethnic_maj_50 Ratio_ethnic_maj_500 Ratio_ethnic_maj_1000 Ratio_ethnic_maj_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_ethnic_maj.pdf", as(pdf) 

collapse Ratio_ethnic_maj_50 Ratio_ethnic_maj_100 Ratio_ethnic_maj_500 Ratio_ethnic_maj_1000 Ratio_ethnic_maj_2000 
ren Ratio_ethnic_maj_50 Ethnic_majority50
ren Ratio_ethnic_maj_100 Ethnic_majority100
ren Ratio_ethnic_maj_500 Ethnic_majority500
ren Ratio_ethnic_maj_1000 Ethnic_majority1000
ren Ratio_ethnic_maj_2000 Ethnic_majority2000
gen id=1
reshape long Ethnic_majority, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Ethnic_majority_left.dta", replace



* Analys highed
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highed_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

gen parti_low = lower(parti_initial)
gen vald_left = 0
replace vald_left = vald==1 & (parti_low =="s" | parti_low =="g" | parti_low =="v")

foreach k in 50 100 500 1000 2000{
bysort kommunkod: egen Highed_`k' = mean(RatioHighed_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen Highed_`k'_pol = mean(RatioHighed_`year'_`k') if vald_left==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_Highed_`k'= Highed_`k'_pol/Highed_`k'
}
collapse Ratio_Highed_50 Ratio_Highed_100 Ratio_Highed_500 Ratio_Highed_1000 Ratio_Highed_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_education_left_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_education_left_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_education_left_`year'.dta"
}

*sum
*scatter Ratio_Highed_50 Ratio_Highed_500 Ratio_Highed_1000 Ratio_Highed_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_highed.pdf", as(pdf) 
collapse Ratio_Highed_50 Ratio_Highed_100 Ratio_Highed_500 Ratio_Highed_1000 Ratio_Highed_2000 
ren Ratio_Highed_50 Highed50
ren Ratio_Highed_100 Highed100
ren Ratio_Highed_500 Highed500
ren Ratio_Highed_1000 Highed1000
ren Ratio_Highed_2000 Highed2000
gen id=1
reshape long Highed, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Highed_left.dta", replace




* Analys highq
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highq_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

gen parti_low = lower(parti_initial)
gen vald_left = 0
replace vald_left = vald==1 & (parti_low =="s" | parti_low =="g" | parti_low =="v")

foreach k in 50 100 500 1000 2000{
bysort kommunkod: egen Highq_`k' = mean(RatioHighq_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen Highq_`k'_pol = mean(RatioHighq_`year'_`k') if vald_left==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_Highq_`k'= Highq_`k'_pol/Highq_`k'
}
collapse Ratio_Highq_50 Ratio_Highq_100 Ratio_Highq_500 Ratio_Highq_1000 Ratio_Highq_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_dispinc_left_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_dispinc_left_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_dispinc_left_`year'.dta"
}
*sum
*scatter Ratio_Highq_50 Ratio_Highq_100 Ratio_Highq_500 Ratio_Highq_1000 Ratio_Highq_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_highq.pdf", as(pdf) 

collapse Ratio_Highq_50 Ratio_Highq_100 Ratio_Highq_500 Ratio_Highq_1000 Ratio_Highq_2000  
ren Ratio_Highq_50 Highq50
ren Ratio_Highq_100 Highq100
ren Ratio_Highq_500 Highq500
ren Ratio_Highq_1000 Highq1000
ren Ratio_Highq_2000 Highq2000
gen id=1
reshape long Highq, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Highq_left.dta", replace




  
***************************
*Right politicians
***************************
clear all

* Analys ethnic majority
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\ethnic_maj_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

gen parti_low = lower(parti_initial)
gen vald_right = 0
replace vald_right = vald==1 & (parti_low =="c" | parti_low =="m" | parti_low =="k" | parti_low =="f")

foreach k in 50 100 500 1000 2000{
bysort kommunkod: egen ethnic_maj_`k' = mean(Ratioethnic_maj_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen ethnic_maj_`k'_pol = mean(Ratioethnic_maj_`year'_`k') if vald_right==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_ethnic_maj_`k'= ethnic_maj_`k'_pol/ethnic_maj_`k'
}
collapse Ratio_ethnic_maj_50 Ratio_ethnic_maj_100 Ratio_ethnic_maj_500 Ratio_ethnic_maj_1000 Ratio_ethnic_maj_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_maj_right_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_maj_right_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_maj_right_`year'.dta"
}
*sum
*scatter Ratio_ethnic_maj_50 Ratio_ethnic_maj_500 Ratio_ethnic_maj_1000 Ratio_ethnic_maj_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_ethnic_maj.pdf", as(pdf) 

collapse Ratio_ethnic_maj_50 Ratio_ethnic_maj_100 Ratio_ethnic_maj_500 Ratio_ethnic_maj_1000 Ratio_ethnic_maj_2000 
ren Ratio_ethnic_maj_50 Ethnic_majority50
ren Ratio_ethnic_maj_100 Ethnic_majority100
ren Ratio_ethnic_maj_500 Ethnic_majority500
ren Ratio_ethnic_maj_1000 Ethnic_majority1000
ren Ratio_ethnic_maj_2000 Ethnic_majority2000
gen id=1
reshape long Ethnic_majority, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Ethnic_majority_right.dta", replace



* Analys highed
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highed_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

gen parti_low = lower(parti_initial)
gen vald_right = 0
replace vald_right = vald==1 & (parti_low =="c" | parti_low =="m" | parti_low =="k" | parti_low =="f")

foreach k in 50 100 500 1000 2000{
bysort kommunkod: egen Highed_`k' = mean(RatioHighed_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen Highed_`k'_pol = mean(RatioHighed_`year'_`k') if vald_right==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_Highed_`k'= Highed_`k'_pol/Highed_`k'
}
collapse Ratio_Highed_50 Ratio_Highed_100 Ratio_Highed_500 Ratio_Highed_1000 Ratio_Highed_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_education_right_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_education_right_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_education_right_`year'.dta"
}

*sum
*scatter Ratio_Highed_50 Ratio_Highed_500 Ratio_Highed_1000 Ratio_Highed_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_highed.pdf", as(pdf) 
collapse Ratio_Highed_50 Ratio_Highed_100 Ratio_Highed_500 Ratio_Highed_1000 Ratio_Highed_2000 
ren Ratio_Highed_50 Highed50
ren Ratio_Highed_100 Highed100
ren Ratio_Highed_500 Highed500
ren Ratio_Highed_1000 Highed1000
ren Ratio_Highed_2000 Highed2000
gen id=1
reshape long Highed, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Highed_right.dta", replace




* Analys highq
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highq_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

gen parti_low = lower(parti_initial)
gen vald_right = 0
replace vald_right = vald==1 & (parti_low =="c" | parti_low =="m" | parti_low =="k" | parti_low =="f")

foreach k in 50 100 500 1000 2000{
bysort kommunkod: egen Highq_`k' = mean(RatioHighq_`year'_`k') 							/*Municipality-mean*/
bysort kommunkod: egen Highq_`k'_pol = mean(RatioHighq_`year'_`k') if vald_right==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_Highq_`k'= Highq_`k'_pol/Highq_`k'
}
collapse Ratio_Highq_50 Ratio_Highq_100 Ratio_Highq_500 Ratio_Highq_1000 Ratio_Highq_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_dispinc_right_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_dispinc_right_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_dispinc_right_`year'.dta"
}
*sum
*scatter Ratio_Highq_50 Ratio_Highq_100 Ratio_Highq_500 Ratio_Highq_1000 Ratio_Highq_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_highq.pdf", as(pdf) 

collapse Ratio_Highq_50 Ratio_Highq_100 Ratio_Highq_500 Ratio_Highq_1000 Ratio_Highq_2000  
ren Ratio_Highq_50 Highq50
ren Ratio_Highq_100 Highq100
ren Ratio_Highq_500 Highq500
ren Ratio_Highq_1000 Highq1000
ren Ratio_Highq_2000 Highq2000
gen id=1
reshape long Highq, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Highq_right.dta", replace





clear all

**********************************
* All politicians: DISADVANTAGE **
**********************************

* Analys ethnic minority
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\ethnic_maj_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

foreach k in 50 100 500 1000 2000{
gen Ratioethnic_min_`year'_`k' = 1-Ratioethnic_maj_`year'_`k'
bysort kommunkod: egen ethnic_min_`k' = mean(Ratioethnic_min_`year'_`k') 							/*Municipality-mean*/
bysort kommunkod: egen ethnic_min_`k'_pol = mean(Ratioethnic_min_`year'_`k') if politician_`year'==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_ethnic_min_`k'= ethnic_min_`k'_pol/ethnic_min_`k'
}
collapse Ratio_ethnic_min_50 Ratio_ethnic_min_100 Ratio_ethnic_min_500 Ratio_ethnic_min_1000 Ratio_ethnic_min_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_min_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_min_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_min_`year'.dta"
}
*sum
*scatter Ratio_ethnic_maj_50 Ratio_ethnic_maj_500 Ratio_ethnic_maj_1000 Ratio_ethnic_min_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_ethnic_min.pdf", as(pdf) 

collapse Ratio_ethnic_min_50 Ratio_ethnic_min_100 Ratio_ethnic_min_500 Ratio_ethnic_min_1000 Ratio_ethnic_min_2000 
ren Ratio_ethnic_min_50 Ethnic_minority50
ren Ratio_ethnic_min_100 Ethnic_minority100
ren Ratio_ethnic_min_500 Ethnic_minority500
ren Ratio_ethnic_min_1000 Ethnic_minority1000
ren Ratio_ethnic_min_2000 Ethnic_minority2000
gen id=1
reshape long Ethnic_minority, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Ethnic_minority.dta", replace




* Analys highed
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highed_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

foreach k in 50 100 500 1000 2000{
gen RatioLowed_`year'_`k' = 1-RatioHighed_`year'_`k'
bysort kommunkod: egen Lowed_`k' = mean(RatioLowed_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen Lowed_`k'_pol = mean(RatioLowed_`year'_`k') if politician_`year'==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_Lowed_`k'= Lowed_`k'_pol/Lowed_`k'
}
collapse Ratio_Lowed_50 Ratio_Lowed_100 Ratio_Lowed_500 Ratio_Lowed_1000 Ratio_Lowed_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_loweducation_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_loweducation_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_loweducation_`year'.dta"
}

*sum
*scatter Ratio_Highed_50 Ratio_Highed_500 Ratio_Highed_1000 Ratio_Lowed_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_Lowed.pdf", as(pdf) 
collapse Ratio_Lowed_50 Ratio_Lowed_100 Ratio_Lowed_500 Ratio_Lowed_1000 Ratio_Lowed_2000 
ren Ratio_Lowed_50 Lowed50
ren Ratio_Lowed_100 Lowed100
ren Ratio_Lowed_500 Lowed500
ren Ratio_Lowed_1000 Lowed1000
ren Ratio_Lowed_2000 Lowed2000
gen id=1
reshape long Lowed, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Lowed.dta", replace




* Analys highq
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highq_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

foreach k in 50 100 500 1000 2000{
gen RatioLowq_`year'_`k' = 1-RatioHighq_`year'_`k'
bysort kommunkod: egen Lowq_`k' = mean(RatioLowq_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen Lowq_`k'_pol = mean(RatioLowq_`year'_`k') if politician_`year'==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_Lowq_`k'= Lowq_`k'_pol/Lowq_`k'
}
collapse Ratio_Lowq_50 Ratio_Lowq_100 Ratio_Lowq_500 Ratio_Lowq_1000 Ratio_Lowq_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_Lowq_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_Lowq_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_Lowq_`year'.dta"
}
*sum
*scatter Ratio_Highq_50 Ratio_Highq_100 Ratio_Highq_500 Ratio_Highq_1000 Ratio_Lowq_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_Lowq.pdf", as(pdf) 

collapse Ratio_Lowq_50 Ratio_Lowq_100 Ratio_Lowq_500 Ratio_Lowq_1000 Ratio_Lowq_2000  
ren Ratio_Lowq_50 Lowq50
ren Ratio_Lowq_100 Lowq100
ren Ratio_Lowq_500 Lowq500
ren Ratio_Lowq_1000 Lowq1000
ren Ratio_Lowq_2000 Lowq2000
gen id=1
reshape long Lowq, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Lowq.dta", replace





******************
* Left politicians
******************

clear all

* Analys ethnic minority
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\ethnic_maj_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

gen parti_low = lower(parti_initial)
gen vald_left = 0
replace vald_left = vald==1 & (parti_low =="s" | parti_low =="g" | parti_low =="v")

foreach k in 50 100 500 1000 2000{
gen Ratioethnic_min_`year'_`k' = 1-Ratioethnic_maj_`year'_`k'
bysort kommunkod: egen ethnic_min_`k' = mean(Ratioethnic_min_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen ethnic_min_`k'_pol = mean(Ratioethnic_min_`year'_`k') if vald_left==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_ethnic_min_`k'= ethnic_min_`k'_pol/ethnic_min_`k'
}
collapse Ratio_ethnic_min_50 Ratio_ethnic_min_100 Ratio_ethnic_min_500 Ratio_ethnic_min_1000 Ratio_ethnic_min_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_min_left_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_min_left_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_min_left_`year'.dta"
}
*sum
*scatter Ratio_ethnic_maj_50 Ratio_ethnic_maj_500 Ratio_ethnic_maj_1000 Ratio_ethnic_min_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_ethnic_min.pdf", as(pdf) 

collapse Ratio_ethnic_min_50 Ratio_ethnic_min_100 Ratio_ethnic_min_500 Ratio_ethnic_min_1000 Ratio_ethnic_min_2000 
ren Ratio_ethnic_min_50 Ethnic_minority50
ren Ratio_ethnic_min_100 Ethnic_minority100
ren Ratio_ethnic_min_500 Ethnic_minority500
ren Ratio_ethnic_min_1000 Ethnic_minority1000
ren Ratio_ethnic_min_2000 Ethnic_minority2000
gen id=1
reshape long Ethnic_minority, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Ethnic_minority_left.dta", replace




* Analys highed
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highed_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

gen parti_low = lower(parti_initial)
gen vald_left = 0
replace vald_left = vald==1 & (parti_low =="s" | parti_low =="g" | parti_low =="v")

foreach k in 50 100 500 1000 2000{
gen RatioLowed_`year'_`k' = 1-RatioHighed_`year'_`k'
bysort kommunkod: egen Lowed_`k' = mean(RatioLowed_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen Lowed_`k'_pol = mean(RatioLowed_`year'_`k') if vald_left==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_Lowed_`k'= Lowed_`k'_pol/Lowed_`k'
}
collapse Ratio_Lowed_50 Ratio_Lowed_100 Ratio_Lowed_500 Ratio_Lowed_1000 Ratio_Lowed_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_loweducation_left_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_loweducation_left_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_loweducation_left_`year'.dta"
}

*sum
*scatter Ratio_Highed_50 Ratio_Highed_500 Ratio_Highed_1000 Ratio_Lowed_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_Lowed.pdf", as(pdf) 
collapse Ratio_Lowed_50 Ratio_Lowed_100 Ratio_Lowed_500 Ratio_Lowed_1000 Ratio_Lowed_2000 
ren Ratio_Lowed_50 Lowed50
ren Ratio_Lowed_100 Lowed100
ren Ratio_Lowed_500 Lowed500
ren Ratio_Lowed_1000 Lowed1000
ren Ratio_Lowed_2000 Lowed2000
gen id=1
reshape long Lowed, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Lowed_left.dta", replace




* Analys highq
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highq_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

gen parti_low = lower(parti_initial)
gen vald_left = 0
replace vald_left = vald==1 & (parti_low =="s" | parti_low =="g" | parti_low =="v")

foreach k in 50 100 500 1000 2000{
gen RatioLowq_`year'_`k' = 1-RatioHighq_`year'_`k'
bysort kommunkod: egen Lowq_`k' = mean(RatioLowq_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen Lowq_`k'_pol = mean(RatioLowq_`year'_`k') if vald_left==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_Lowq_`k'= Lowq_`k'_pol/Lowq_`k'
}
collapse Ratio_Lowq_50 Ratio_Lowq_100 Ratio_Lowq_500 Ratio_Lowq_1000 Ratio_Lowq_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_Lowq_left_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_Lowq_left_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_Lowq_left_`year'.dta"
}
*sum
*scatter Ratio_Highq_50 Ratio_Highq_100 Ratio_Highq_500 Ratio_Highq_1000 Ratio_Lowq_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_Lowq.pdf", as(pdf) 

collapse Ratio_Lowq_50 Ratio_Lowq_100 Ratio_Lowq_500 Ratio_Lowq_1000 Ratio_Lowq_2000  
ren Ratio_Lowq_50 Lowq50
ren Ratio_Lowq_100 Lowq100
ren Ratio_Lowq_500 Lowq500
ren Ratio_Lowq_1000 Lowq1000
ren Ratio_Lowq_2000 Lowq2000
gen id=1
reshape long Lowq, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Lowq_left.dta", replace







******************
* Right politicians
******************

clear all

* Analys ethnic minority
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\ethnic_maj_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

gen parti_low = lower(parti_initial)
gen vald_right = 0
replace vald_right = vald==1 & (parti_low =="c" | parti_low =="m" | parti_low =="k" | parti_low =="f")

foreach k in 50 100 500 1000 2000{
gen Ratioethnic_min_`year'_`k' = 1-Ratioethnic_maj_`year'_`k'
bysort kommunkod: egen ethnic_min_`k' = mean(Ratioethnic_min_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen ethnic_min_`k'_pol = mean(Ratioethnic_min_`year'_`k') if vald_right==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_ethnic_min_`k'= ethnic_min_`k'_pol/ethnic_min_`k'
}
collapse Ratio_ethnic_min_50 Ratio_ethnic_min_100 Ratio_ethnic_min_500 Ratio_ethnic_min_1000 Ratio_ethnic_min_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_min_right_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_min_right_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_ethnic_min_right_`year'.dta"
}
*sum
*scatter Ratio_ethnic_maj_50 Ratio_ethnic_maj_500 Ratio_ethnic_maj_1000 Ratio_ethnic_min_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_ethnic_min.pdf", as(pdf) 

collapse Ratio_ethnic_min_50 Ratio_ethnic_min_100 Ratio_ethnic_min_500 Ratio_ethnic_min_1000 Ratio_ethnic_min_2000 
ren Ratio_ethnic_min_50 Ethnic_minority50
ren Ratio_ethnic_min_100 Ethnic_minority100
ren Ratio_ethnic_min_500 Ethnic_minority500
ren Ratio_ethnic_min_1000 Ethnic_minority1000
ren Ratio_ethnic_min_2000 Ethnic_minority2000
gen id=1
reshape long Ethnic_minority, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Ethnic_minority_right.dta", replace




* Analys highed
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highed_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

gen parti_low = lower(parti_initial)
gen vald_right = 0
replace vald_right = vald==1 & (parti_low =="c" | parti_low =="m" | parti_low =="k" | parti_low =="f")

foreach k in 50 100 500 1000 2000{
gen RatioLowed_`year'_`k' = 1-RatioHighed_`year'_`k'
bysort kommunkod: egen Lowed_`k' = mean(RatioLowed_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen Lowed_`k'_pol = mean(RatioLowed_`year'_`k') if vald_right==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_Lowed_`k'= Lowed_`k'_pol/Lowed_`k'
}
collapse Ratio_Lowed_50 Ratio_Lowed_100 Ratio_Lowed_500 Ratio_Lowed_1000 Ratio_Lowed_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_loweducation_right_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_loweducation_right_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_loweducation_right_`year'.dta"
}

*sum
*scatter Ratio_Highed_50 Ratio_Highed_500 Ratio_Highed_1000 Ratio_Lowed_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_Lowed.pdf", as(pdf) 
collapse Ratio_Lowed_50 Ratio_Lowed_100 Ratio_Lowed_500 Ratio_Lowed_1000 Ratio_Lowed_2000 
ren Ratio_Lowed_50 Lowed50
ren Ratio_Lowed_100 Lowed100
ren Ratio_Lowed_500 Lowed500
ren Ratio_Lowed_1000 Lowed1000
ren Ratio_Lowed_2000 Lowed2000
gen id=1
reshape long Lowed, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Lowed_right.dta", replace




* Analys highq
foreach year in 2002 2006 2010 {
clear
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\highq_`year'_temp.dta"
merge 1:m XKOORD_`year' YKOORD_`year' using "\\micro.intra\Projekt\P0624$\P0624_Gem\Politisk Geografi\Data\MergedData\Merged_`year'.dta"
drop _merge

gen parti_low = lower(parti_initial)
gen vald_right = 0
replace vald_right = vald==1 & (parti_low =="c" | parti_low =="m" | parti_low =="k" | parti_low =="f")

foreach k in 50 100 500 1000 2000{
gen RatioLowq_`year'_`k' = 1-RatioHighq_`year'_`k'
bysort kommunkod: egen Lowq_`k' = mean(RatioLowq_`year'_`k') 							/*Municipality-mean */
bysort kommunkod: egen Lowq_`k'_pol = mean(RatioLowq_`year'_`k') if vald_right==1		/*Ditto for politicians*/
bysort kommunkod: gen Ratio_Lowq_`k'= Lowq_`k'_pol/Lowq_`k'
}
collapse Ratio_Lowq_50 Ratio_Lowq_100 Ratio_Lowq_500 Ratio_Lowq_1000 Ratio_Lowq_2000 
gen year = `year'
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_Lowq_right_`year'.dta", replace
}

use "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_Lowq_right_2002.dta", clear
foreach year in 2006 2010 {
append using "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\k_nhood_Lowq_right_`year'.dta"
}
*sum
*scatter Ratio_Highq_50 Ratio_Highq_100 Ratio_Highq_500 Ratio_Highq_1000 Ratio_Lowq_2000 year
*graph export "\\micro.intra\mydocs\mydocs\dahma\my documents\OutBox\k_nearest_Lowq.pdf", as(pdf) 

collapse Ratio_Lowq_50 Ratio_Lowq_100 Ratio_Lowq_500 Ratio_Lowq_1000 Ratio_Lowq_2000  
ren Ratio_Lowq_50 Lowq50
ren Ratio_Lowq_100 Lowq100
ren Ratio_Lowq_500 Lowq500
ren Ratio_Lowq_1000 Lowq1000
ren Ratio_Lowq_2000 Lowq2000
gen id=1
reshape long Lowq, i(id) j(k)
sum
save "\\micro.intra\projekt\P0624$\P0624_Gem\Politisk Geografi\Data\temp\Lowq_right.dta", replace



log close

